function setEditableTaskID(id)
{
	//alert(id);
	if(id.length > 0 && id != "0")
	{
		//set current editable taskid
		$("div#content input.hide_current_id").val(id);
		//alert($("div#content input.hide_current_id").val());
	}
	
}

function getEditableTaskID()
{
	$id = $("div#content input.hide_current_id").val();
	
	if($id.length > 0 && $id != "0")
	{
		return $id;
	}
	
	return 0;
}

function php_addtask(form,messageReceiver)
{
	debugger;
	
	//get all the inputs and send to addtask.php
	var $name = form.find("input.task-name").val();
	var $earn = form.find("input.set-coin").val();
	var year = form.find("select[name=year]").val();
	var month = form.find("select[name=month]").val();
	var date = form.find("select[name=date]").val();
	var $dueDate = year + '-' + month + '-' + date;
	var $type;
	var $tag = form.find("input.tag").val();
	var $url = form.find("input.url").val();
	
	
	var $repeat = $("input[name=repeat]");
	$repeat.each(function(){
		if (this.checked){
			$pObj = $(this).parent();
			$type = $pObj.text().toLowerCase();
		}
	});
	
	//clear the value of input tag
	form.find("input.tag").val("");
	
	messageReceiver.load("dataLogic/operation_task.php",{"method" : "add" ,"name" : $name, "points" : $earn, "type" : $type , "dueDate" : $dueDate,"tag":$tag,"url":$url });
	reloadpage_task();
}

function php_updatetask(form,messageReceiver)
{
	//get all the inputs and send to addtask.php
	debugger;
	var $id = getEditableTaskID();
	var $name = form.find("input.task-name").val();
	var $earn = form.find("input.set-coin").val();
	var year = form.find("select[name=year]").val();
	var month = form.find("select[name=month]").val();
	var date = form.find("select[name=date]").val();
	var $dueDate = year + '-' + month + '-' + date;
	var $type;
	var $tag = form.find("input.tag").val();
	var $url = form.find("input.url").val();
	
	//get task type 
	var $repeat = $("input[name=repeat]");
	$repeat.each(function(){
		if (this.checked){
			$pObj = $(this).parent();
			$type = $pObj.text().toLowerCase();
		}
	});
	
	messageReceiver.load("dataLogic/operation_task.php",{"method" : "edit" ,"id": $id , "name" : $name, "points" : $earn, "type" : $type , "dueDate" : $dueDate,"tag": $tag,"url" :$url  });
	reloadpage_task();
}

function php_deletetask(messageReceiver)
{
	var $id = getEditableTaskID();
	messageReceiver.load("dataLogic/operation_task.php",{"method" : "delete" ,"id" : $id });
	reloadpage_task();
}

function php_finishtask(li,messageReceiver)
{
	debugger;
	points = li.find("span.coin").text();
	id = li.find("div.operation input.hidden_id").val();
	
	messageReceiver.load("dataLogic/finishtask.php?id="+id+"&points="+points);
	reloadpage_task();
	reloadpage_header();
}

function php_addreward(form,messageReceiver)
{
	//get all the inputs and send to addtask.php
	var $name = form.find("input.reward-name").val();
	var $earn = form.find("input.set-coin").val();
	var $url = form.find("input.url").val();
		
	messageReceiver.load("dataLogic/operation_reward.php",{"method" : "add" , "name" : $name, "points" : $earn,"url" : $url });
	reloadpage_reward();
}

function php_updatereward(form,messageReceiver)
{
	//get all the inputs and send to addtask.php
	debugger;
	var $id = getEditableTaskID();
	var $name = form.find("input.reward-name").val();
	var $earn = form.find("input.set-coin").val();
	var $url = form.find("input.url").val();
	
	messageReceiver.load("dataLogic/operation_reward.php",{"method" : "edit" ,"id": $id , "name" : $name, "points" : $earn,"url" :$url });
	reloadpage_reward();
}

function php_deletereward(messageReceiver)
{
	debugger;
	var $id = getEditableTaskID();
	messageReceiver.load("dataLogic/operation_reward.php",{"method" : "delete" ,"id" : $id });
	reloadpage_reward();
}

function php_getreward(points,messageReceiver)
{
	var $id = getEditableTaskID();
	messageReceiver.load("dataLogic/getreward.php?id="+ $id +"&points=" +points);
	reloadpage_reward();
	reloadspan_coinnumber();
	reloadpage_header();
}

function php_logout($link)
{
	//this variable has no meaning at all.
	$link.load("dataLogic/logout.php",function(){
		window.location = "./login.htm";
	});
	
}

function reloadspan_unfinishCount()
{
	//alert("reload dataLogic/unfinished_task_count.php");
	$("span.count").load("dataLogic/unfinished_task_count.php");
}

function reloadspan_coinnumber()
{
	$("span.coin-number").load("dataLogic/earnpoints.php");
}

function reloadpage_task()
{
	$("div#tasklist").load("dataLogic/tasklist.php");

	$("div#taglist").load("dataLogic/taglist.php");
	
	reloadspan_unfinishCount();
}

function reloadpage_reward()
{
	$("div#rewardlist").load("dataLogic/rewardlist.php");
}

function reloadpage_header()
{
	$("div.header").load("pageheader.php");
}
